package com.licw.controller.admin;

import com.licw.constant.StatusConstant;
import com.licw.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

// 给bean取名区别于user下的ShopController
@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店铺操作接口")
@Slf4j
public class ShopController {

    private static final String SHOP_STATUS = "shop_status";

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @PutMapping("/{status}")
    @ApiOperation("设置营业状态")
    public Result<Object> setShopStatus(@PathVariable Integer status) {
        log.info("设置营业状态：{}", status == 0 ? "打烊中" : "营业中");
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(SHOP_STATUS, StatusConstant.ENABLE);
        return Result.success();
    }

    @GetMapping("/status")
    @ApiOperation("获取营业状态")
    public Result<Integer> setShopStatus() {
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        Integer status = (Integer) valueOperations.get(SHOP_STATUS);
        if(status == null) status = 0;
        log.info("获取到营业状态：{}", status == 0 ? "打烊中" : "营业中");
        return Result.success(status);
    }
}